package com.shockweb.common.calculate.function;

import com.shockweb.common.calculate.CalculateException;
import com.shockweb.common.calculate.api.IFunction;

import java.math.BigDecimal;
import java.util.List;

/**
 * Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. The integer returned is the smallest value k for which: 
 * @author pengminghua
 *
 */
public class IndexOf implements IFunction{
	
	/**
	 * @see IFunction#eval(List)
	 */
	public BigDecimal eval(List<Object> params) throws CalculateException {
		try{
			Object value = params.get(0);
			Object str = params.get(1);
			Object k = params.get(2);
			int start = k instanceof BigDecimal?((BigDecimal)k).intValue():Integer.parseInt(k.toString());
			if(value==null){
				throw new NullPointerException();
			}else{
				return new BigDecimal(((String)value).indexOf((String)str, start));
			}
		}catch(Exception e){
			throw new CalculateException(e);
		}
	}


	/**
	 * @see IFunction#getParamCount()
	 */
	public int getParamCount() {
		return 3;
	}
	
	/**
	 * @see IFunction#getName()
	 */
	public String getName() {
		return "indexOf";
	}
}
